'use strict';

import { Map, fromJS } from 'immutable';
import { Action, Actor } from 'iflux2';
import RegionCascader from '../component/region-cascader';
import RangeInputNumber from '../component/range-input-number';
import RangeDatePicker from '../component/range-date-picker';
import moment from 'moment';
import * as types from '../constants/actionTypes';

export default class FilterActor extends Actor {
  defaultState() {
    return {
      last: fromJS({ domainType: '0032' }), //最终的请求参数
      totalCount:0,
      touser: [], //获得的粉丝列表
      conditionList: [
        {
          label: '性别',
          key: 'sex',
          conditionList: [
            {
              key: 'sex',
              label: '不限',
              checked: true,
              value: null
            },
            {
              key: 'sex',
              label: '男',
              checked: false,
              value: 'M'
            },
            {
              key: 'sex',
              label: '女',
              checked: false,
              value: 'F'
            },
            {
              key: 'sex',
              label: '保密',
              checked: false,
              value: 'NA'
            }
          ],
          addCondition: null
        },
        {
          label: '地区',
          conditionList: [],
          addCondition: {
            reactDom: RegionCascader,
            props: {
              keys: 'province,city'
            }
          }
        },
        {
          label: '消费均价',
          key: 'tradeAverageFee',
          conditionList: [
            {
              key: 'tradeAverageFee',
              checked: true,
              label: '不限',
              value: {
                from: null,
                to: null
              }
            },
            {
              key: 'tradeAverageFee',
              checked: false,
              label: '0',
              value: {
                from: null,
                to: 1
              }
            },
            {
              key: 'tradeAverageFee',
              checked: false,
              label: '1-100',
              value: {
                from: 1,
                to: 100
              }
            },
            {
              key: 'tradeAverageFee',
              checked: false,
              label: '100-200',
              value: {
                from: 100,
                to: 200
              }
            },
            {
              key: 'tradeAverageFee',
              checked: false,
              label: '200-300',
              value: {
                from: 200,
                to: 300
              }
            },
            {
              key: 'tradeAverageFee',
              checked: false,
              label: '300-400',
              value: {
                from: 300,
                to: 400
              }
            },
            {
              key: 'tradeAverageFee',
              checked: false,
              label: '400-500',
              value: {
                from: 400,
                to: 500
              }
            },
            {
              key: 'tradeAverageFee',
              checked: false,
              label: '500以上',
              value: {
                from: 500,
                to: null
              }
            }
          ],
          addCondition: {
            reactDom: RangeInputNumber,
            props: {
              extendInfo: {
                suffix: '以上',
                step: 100
              },
              demo: {
                key: 'tradeAverageFee',
                checked: true,
                label: '',
                value: {
                  from: '',
                  to: ''
                }
              }
            }
          }
        },
        {
          label: '消费频次',
          key: 'tradeFrequency',
          conditionList: [
            {
              key: 'tradeFrequency',
              checked: true,
              label: '不限',
              value: {
                from: null,
                to: null
              }
            },
            {
              key: 'tradeFrequency',
              checked: false,
              label: '0',
              value: {
                from: 0,
                to: 1
              }
            },
            {
              key: 'tradeFrequency',
              checked: false,
              label: '1',
              value: {
                from: 1,
                to: 2
              }
            },
            {
              key: 'tradeFrequency',
              checked: false,
              label: '2',
              value: {
                from: 2,
                to: 3
              }
            },
            {
              key: 'tradeFrequency',
              checked: false,
              label: '3',
              value: {
                from: 3,
                to: 4
              }
            },
            {
              key: 'tradeFrequency',
              checked: false,
              label: '4',
              value: {
                from: 4,
                to: 5
              }
            },
            {
              key: 'tradeFrequency',
              checked: false,
              label: '5',
              value: {
                from: 5,
                to: 6
              }
            },
            {
              key: 'tradeFrequency',
              checked: false,
              label: '6笔及以上',
              value: {
                from: 6,
                to: null
              }
            }
          ],
          addCondition: {
            reactDom: RangeInputNumber,
            props: {
              extendInfo: {
                suffix: '笔及以上',
                step: 1,
                iconMessage: `消费频次范围设置：
1.左右的输入框分别是范围的下界和上界，如输入 “5-8”，对应的是消费频次是5、6、7、8的公众号粉丝
2.筛选“几笔以上”，仅在左边输入数值即可，如“10 - ”将筛选10笔以上（包含10）的公众号粉丝
3.筛选“几笔以下”，仅在右边输入数值即可，如“ - 10”将筛选10笔以下（包含10，包含0）的公众号粉丝`
              },
              demo: {
                key: 'tradeFrequency',
                checked: true,
                label: '',
                value: {
                  from: '',
                  to: ''
                }
              }
            }
          }
        },
        {
          label: '最近消费',
          key: 'lastTradeTime',
          conditionList: [
            {
              key: 'lastTradeTime',
              checked: true,
              label: '不限',
              value: {
                from: null,
                to: null
              }
            },
            {
              label: '1周内',
              key: 'lastTradeTime',
              checked: false,
              value: {
                from: generateRangeDate({ week: 1 }),
                to: generateRangeDate()
              }
            },
            {
              label: '2周内',
              key: 'lastTradeTime',
              checked: false,
              value: {
                from: generateRangeDate({ week: 2 }),
                to: generateRangeDate()
              }
            },
            {
              label: '1个月内',
              key: 'lastTradeTime',
              checked: false,
              value: {
                from: generateRangeDate({ month: 1 }),
                to: generateRangeDate()
              }
            },
            {
              key: 'lastTradeTime',
              label: '2个月内',
              checked: false,
              value: {
                from: generateRangeDate({ month: 2 }),
                to: generateRangeDate()
              }
            }
          ],
          addCondition: {
            reactDom: RangeDatePicker,
            props: {
              demo: {
                key: 'lastTradeTime',
                checked: true,
                label: '',
                value: {
                  from: '',
                  to: ''
                }
              }
            }
          }
        },
        {
          label: '积分',
          key: 'integral',
          conditionList: [
            {
              key: 'integral',
              checked: true,
              label: '不限',
              value: {
                from: null,
                to: null
              }
            },
            {
              key: 'integral',
              checked: false,
              label: '0-100',
              value: {
                from: 0,
                to: 100
              }
            },
            {
              key: 'integral',
              checked: false,
              label: '100-200',
              value: {
                from: 100,
                to: 200
              }
            },
            {
              key: 'integral',
              checked: false,
              label: '200-300',
              value: {
                from: 200,
                to: 300
              }
            },
            {
              key: 'integral',
              checked: false,
              label: '300-400',
              value: {
                from: 300,
                to: 400
              }
            },
            {
              key: 'integral',
              checked: false,
              label: '400-500',
              value: {
                from: 400,
                to: 500
              }
            }
          ],
          addCondition: {
            reactDom: RangeInputNumber,
            props: {
              extendInfo: {
                suffix: '以上',
                step: 100
              },
              demo: {
                key: 'integral',
                checked: true,
                label: '',
                value: {
                  from: '',
                  to: ''
                }
              }
            }
          }
        },
        {
          label: '关注时间',
          key: 'followTime',
          conditionList: [
            {
              key: 'followTime',
              checked: true,
              label: '不限',
              value: {
                from: null,
                to: null
              }
            },
            {
              label: '1周内',
              key: 'followTime',
              checked: false,
              value: {
                from: generateRangeDate({ week: 1 }),
                to: generateRangeDate()
              }
            },
            {
              label: '2周内',
              key: 'followTime',
              checked: false,
              value: {
                from: generateRangeDate({ week: 2 }),
                to: generateRangeDate()
              }
            },
            {
              label: '1个月内',
              key: 'followTime',
              checked: false,
              value: {
                from: generateRangeDate({ month: 1 }),
                to: generateRangeDate()
              }
            },
            {
              key: 'followTime',
              label: '2个月内',
              checked: false,
              value: {
                from: generateRangeDate({ month: 2 }),
                to: generateRangeDate()
              }
            }
          ],
          addCondition: {
            reactDom: RangeDatePicker,
            props: {
              demo: {
                key: 'followTime',
                checked: true,
                label: '',
                value: {
                  from: '',
                  to: ''
                }
              }
            }
          }
        }
      ]
    };
  }

  /**
	 * 追加一个条件 并选中
	 * @param state
	 * @param result
	 * @returns {*}
	 */
  @Action(types.ADD_CONDITION)
  addCondition(state, result) {
    let { value: demo, path } = result;
    let { key, label, value: { from, to } } = demo;
    if (key === 'tradeFrequency') {
      if (isNum(from) && isNum(to) && from === to) {
        to += 1;
        label = from + '';
      } else if (isNum(from) && isNum(to) && from !== to) {
        to += 1;
      }
    } else {
      if (isNum(from) && isNum(to) && from === to) {
        to += 1;
        label = `${from}-${to}`;
      }
    }
    Object.assign(demo, { label, value: { from, to } });
    return state.setIn(['conditionList'].concat(path), demo).setIn(['last', key], { from, to });
  }

  /**
	 * 选中某个条件
	 * @param state
	 * @param result
	 * @returns {*}
	 */
  @Action(types.SELECT_ITEM)
  selectItem(state, result) {
    let { key, value } = result;
    return state.setIn(['last', key], value);
  }

  @Action(types.FETCH_FANS)
  fetchFans(state, touser) {
    return state.set('touser', touser);
  }
  @Action(types.FETCH_FANS_COUNT)
  fetchFansCount(state,count){
	  return state.set('totalCount', count);
  }
}
/**
 *
 * @param info eg:{days:7,months:2}
 * @returns {*}
 */
function generateRangeDate(info) {
  return info ? moment().subtract(info).format('YYYY-MM-DD') : moment().format('YYYY-MM-DD');
}

function isNum(n) {
  return typeof n === 'number';
}
